package com.cheung.leetcode.contest.week.week403;

import org.junit.jupiter.api.Test;

/**
 * @BelongsProject : java-leetcode
 * @BelongsPackage : com.cheung.leetcode.contest.week403
 * @Author :  cheungming
 * @CreateTime : 2024-06-24 16:40:11
 * @Description : 第二题
 * @Version : 1.0
 */
public class Code3195Test {
    public int minimumArea(int[][] grid) {
        int row = grid.length, col = grid[0].length;

        // 设x,y为左上角坐标，m,n为右下角坐标,则初始值为
        int x = 0, y = 0;
        int m = row - 1, n = grid[0].length - 1;

        // 遍历行
        for (int[] arr : grid) {
            int s = 0;
            for (int j = 0; j < col; j++) {
                s += arr[j];
            }

            if (s == 0) {
                x++;
            } else {
                break;
            }

        }

        for (int i = row - 1; i >= 0; i--) {
            int s = 0;
            for (int j = 0; j < col; j++) {
                s += grid[i][j];
            }

            if (s == 0) {
                m--;
            } else {
                break;
            }
        }


        for (int j = 0; j < col; j++) {
            int s = 0;
            for (int[] arr : grid) {
                s += arr[j];
            }
            if (s == 0) {
                y++;
            } else {
                break;
            }
        }

        for (int j = col - 1; j >= 0; j--) {
            int t = 0;
            for (int[] arr : grid) {
                t += arr[j];
            }
            if (t == 0) {
                n--;
            } else {
                break;
            }
        }

        System.out.println("x=" + x + ",y=" + y);
        System.out.println("m=" + m + ",n=" + n);


        return (m - x + 1) * (n - y + 1);
    }

    @Test
    public void test1() {
        assert minimumArea(new int[][]{{0, 1, 0}, {1, 0, 1}}) == 6;
    }

    @Test
    public void test2() {
        assert minimumArea(new int[][]{{0, 0}, {1, 0}}) == 1;
    }
}
